package com.excel.read;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.tags.TableColumn;

import com.excel.CELL;
import com.excel.TableModel;

import jxl.Cell;
import jxl.Range;
import jxl.Sheet;
import jxl.read.biff.BiffException;
import jxl.read.biff.BlankCell;

public class ExcelConfig {
	ExcelTableManager pmbManager = new TableManagerImpl();
	/**
	 * 跨行跨列的单元格
	 */
	public Map<String, Range> rangeMap = new HashMap<String, Range>();
	

	
	public String tableConfig(String realPath) {
		return realPath;
	}

	protected void getMulCell(Sheet s) {
		Range [] array = s.getMergedCells();		
		for(Range aRange : array) {
			Cell topLeft = aRange.getTopLeft();
			String key =topLeft.getRow()+"_"+topLeft.getColumn();
			rangeMap.put(key, aRange);
		}
		
	}
	
	private boolean isMarked(int rowIndex, int colIndex, TableModel aTableModel) {
		CELL aCELL2 = aTableModel.cells[rowIndex][colIndex];  
		 if(aCELL2.visitFlag) {
			 return true;
		 }
		 return false;
	}
	
	 /**
	  * 标记并且合并单元格
	  * @param s
	  * @param i
	  * @param j
	  * @param rowIndex
	  * @param colIndex
	  * @param rows
	  * @param cols
	  * @param aTableModel
	  * @param tdElement
	  */   
	 private void mark(Sheet s, int i, int j, int rowIndex, int colIndex, int rows, int cols, TableModel aTableModel, TableColumn tdElement) {    	
		     CELL aCELL2 = aTableModel.cells[rowIndex][colIndex];  
			 if(aCELL2.visitFlag) {
				 return;
			 }
			 
	    	Cell cell = s.getCell(j, i); 
	    	String key =cell.getRow()+"_"+cell.getColumn();
	    	int dltRow = 0;
	    	int dltCol = 0;
	    	Range aRange = rangeMap.get(key);
	    	if(aRange == null) {
	    		CELL aCELL = aTableModel.cells[rowIndex][colIndex];    			
				aCELL.visitFlag = true;
				aCELL.cellPosition = ""+(rowIndex)+"_"+(colIndex);			
				aCELL.aTD = tdElement;	
				
				//标记属性之用
			 	String content = tdElement.getAttribute("vv");
				if(content != null && !content.equals("")) {
					aTableModel.putAtr(content, aCELL);
				}
				
	    	}else {
	    		Cell topLeft = aRange.getTopLeft();
	    		Cell bottomRight = aRange.getBottomRight();
	    		dltRow = bottomRight.getRow() - topLeft.getRow() + 1;
	    		dltCol = bottomRight.getColumn() - topLeft.getColumn() + 1;
	    		//从左到右，从上到下
	        	for(int m=0; m<dltRow; m++) {
	        		for(int n=0; n<dltCol; n++) {        		
	        			CELL aCELL = aTableModel.cells[rowIndex+m][colIndex+n];    			
	        			aCELL.visitFlag = true;
	        			aCELL.cellPosition = ""+(rowIndex)+"_"+(colIndex);
	        			if(m==0 && n == 0) {
	        				aCELL.aTD = tdElement;
	        				//标记属性之用
	        			 	String content = tdElement.getAttribute("vv");
	        				if(content != null && !content.equals("")) {
	        					aTableModel.putAtr(content, aCELL);
	        				}
	        			}
	            	}
	        	}
	    	}	
	    }
	    
	 
	 public TableModel writeTable(Cell aCell, Sheet s) throws BiffException,
		IOException, SameNameException {
		//GridTable table = new GridSearchTable();
		int rowNum = getRowNum(s, aCell);

		int colNum = getColNum(s, aCell);
		
		TableModel aTableModel = new TableModel(rowNum, colNum);
		
		aTableModel.rowIndex = aCell.getRow();
		aTableModel.colIndex = aCell.getColumn() - 1;
		aTableModel.cols = colNum;
		aTableModel.rows = rowNum;
		aCell = s.getCell(aTableModel.colIndex, aTableModel.rowIndex - 1);

		String aContent = aCell.getContents();
		String strs[]  = aContent.split("\\.");
		aTableModel.name = strs[0];
		//aTableModel.data = new String[aTableModel.rows][aTableModel.cols];
		int i = 0;
		int j = 0;
		for (i = 0; i < aTableModel.rows; i++) {
			for (j = 0; j < aTableModel.cols; j++) {
				Cell cell = s.getCell(j + aTableModel.colIndex, i
						+ aTableModel.rowIndex);
				String content = cell.getContents();
				//table.data[i][j] = content;
				
				
				TableColumn tdElement = new TableColumn();
				tdElement.setAttribute("vv", content);	
				mark(s, i+aTableModel.rowIndex,j + aTableModel.colIndex, i, j, aTableModel.rows, aTableModel.cols, aTableModel, tdElement);
			}
		}
		return aTableModel;
	}
	 

	public static boolean isBlank(Cell cell) {
		boolean flag = false;
		if(cell.getClass().toString().indexOf("BlankCell")>-1) {
			flag = true;
		}
		return flag;
	}
	public static List<Cell> findTables(Sheet s) {
		List<Cell> list = new ArrayList<Cell>();
		int curRowIndex = 1;
		int curColIndex = 0;
		int rows = s.getRows();
		int cols = s.getColumns();
		while (curRowIndex < s.getRows()) {
			Cell cell = s.getCell(2, curRowIndex);
			String content = cell.getContents();
			if (content != null && !content.trim().equals("")) {
				Cell aCell = s.getCell(2, curRowIndex - 1);
				content = aCell.getContents();
				if (content == null || content.trim().equals("")) {
					list.add(cell);
				}
			}
			curRowIndex++;
		}
		return list;
	}
	
	public static int getRowNum(Sheet s, Cell aCell) {
		int row = s.getRows();
		int i = 0;
		for (i = aCell.getRow(); i < s.getRows(); i++) {
			Cell cell = s.getCell(1, i);
			String content = cell.getContents();
			if (content == null || content.trim().equals("")) {
				row = i - aCell.getRow();
				break;
			}
		}
		if (i == s.getRows()) {
			row = i - aCell.getRow();
		}
		return row;
	}

	public int getColNum(Sheet s, Cell aCell) {
		int columns = s.getColumns();
		for (int j = 1; j < s.getColumns(); j++) {
			Cell cell = s.getCell(j, aCell.getRow());
			Cell cell2 = s.getCell(4, 4);
			String content = cell.getContents();
			if (content == null || content.trim().equals("")) {
				columns = j;
				break;
			}

		}
		return columns;
	}


}
